<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 8. All purposes container</a><br />
<a href="list_stack.html">&lt; 8.4 Basic stack operations</a>
</td><td width="60%">
<p align="center">
=====================================<br />
8.5 Iterators<br />
=====================================<br />
</p></td><td width="20%">
<a href="list_delete.html">&gt; 8.6 Deleting a list</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
You can iterate through the elements of the list using an iterator. begin() returns the address of the first element of the list. You go to the next element using the increment operator ++. When the iterator's value is equal to end(), you've gone through all the elements.<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; T * TCODList::begin() const
      template &lt;class T&gt; T * TCODList::end() const
C   : void ** TCOD_list_begin(TCOD_list_t l)
      void ** TCOD_list_end(TCOD_list_t l)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList; // the list is empty (contains 0 elements)
      intList.push(5); // the list contains 1 element at position 0, value = 5
      intList.push(2); // the list contains 2 elements : 5,2
      for ( int * iterator = intList.begin(); iterator != intList.end(); iterator ++ ) {
           int currentValue=*iterator;
           printf("value : %d\n", currentValue );
      }
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_push(intList,(const void *)5);
      TCOD_list_push(intList,(const void *)2);
      for ( int * iterator = (int *)TCOD_list_begin(intList); iterator != (int *)TCOD_list_end(intList); iterator ++ ) {
           int currentValue=*iterator;
           printf("value : %d\n", currentValue );
      }
</pre></p>
<br />
Warning ! You cannot insert elements in the list while iterating through it. Inserting elements can result in reallocation of the list and your iterator will not longer be valid.<br /><hr />
You can remove an element from the list while iterating. The element at the iterator position will be removed. The function returns the new iterator.<br />
<p class="code"><pre>
C++ : template &lt;class T&gt; T *TCODList::remove(T *iterator)
      template &lt;class T&gt; T *TCODList::removeFast(T *iterator)
C   : void **TCOD_list_remove_iterator(TCOD_list_t l, void **iterator)
      void **TCOD_list_remove_iterator_fast(TCOD_list_t l, void **iterator)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>iterator</td><td>The list iterator.</td></tr>
<tr><td>l</td><td>In the C version, the list handler, returned by <a href="list_init.html">a constructor</a>.</td></tr>
</table>
The _fast versions replace the element to remove with the last element of the list. They're faster, but do not preserve the list order.
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODList&lt;int&gt; intList; // the list is empty (contains 0 elements)
      intList.push(5); // the list contains 1 element at position 0, value = 5
      intList.push(2); // the list contains 2 elements : 5,2
      intList.push(3); // the list contains 3 elements : 5,2,3
      for ( int * iterator = intList.begin(); iterator != intList.end(); iterator ++ ) {
           int currentValue=*iterator;
           if ( currentValue == 2 ) {
               // remove this value from the list and keep iterating on next element (value == 3)
               iterator = intList.remove(iterator);
           }
           printf("value : %d\n", currentValue ); // all 3 values will be printed : 5,2,3
      }
      // now the list contains only two elements : 5,3
C   : TCOD_list_t intList = TCOD_list_new();
      TCOD_list_push(intList,(const void *)5);
      TCOD_list_push(intList,(const void *)2);
      TCOD_list_push(intList,(const void *)3);
      for ( int * iterator = (int *)TCOD_list_begin(intList); iterator != (int *)TCOD_list_end(intList); iterator ++ ) {
           int currentValue=*iterator;
           if ( currentValue == 2 ) {
               iterator = (int *)TCOD_list_remove_iterator(intList,(void **)iterator);
           }
           printf("value : %d\n", currentValue );
      }
</pre></p>
